//
//  UITextField+Extension.m
//  DADReadTool
//
//  Created by WanJiading on 15/5/26.
//  Copyright (c) 2015年 DAD. All rights reserved.
//

#import "UITextField+Extension.h"

@implementation UITextField (Extension)

//static CGFloat _textFieldHeight = 46;
static CGFloat _cornerRadius = 3 ;   // 圆角大小
static CGFloat _borderWidth = 0.7;    // 边框大小


/**
 *  设置文本框的样式
 *
 *  @param placeholder   站位文字
 */
- (void)textFieldWitPlaceholder:(NSString *)placeholder {
    
//    [self setBorderWidthAndCornerRadius];
	
    // 文本框的颜色
//    UIColor *color = [UIColor rgb:@"84a740" alpha:1];
//    self.layer.borderColor = [color CGColor];
	
    // 占位文字
//    self.attributedPlaceholder = [[NSAttributedString alloc] initWithString:placeholder attributes:@{NSForegroundColorAttributeName: color}];
}

- (void)setBorderWidthAndCornerRadius {
    
    // 设置文本框边框属性
    self.layer.borderWidth = 2.0 / [UIScreen mainScreen].scale;
    self.layer.cornerRadius = 3.0 / [UIScreen mainScreen].scale;
}

/**
 *  设置文本框的样式
 *
 *  @param placeholder   站位文字
 */
- (void)textFieldWitPlaceholder:(NSString *)placeholder borderColor:(UIColor *)borderColor {

    // 文本框的颜色
    UIColor *color = borderColor;
    self.layer.borderColor = [color CGColor];
    
    // 设置文本框边框属性
    self.layer.borderWidth = _borderWidth;
    self.layer.cornerRadius = _cornerRadius;

    self.attributedPlaceholder = [[NSAttributedString alloc] initWithString:placeholder attributes:@{NSForegroundColorAttributeName: color}];
}


/**
 *  设置文本框的样式
 *
 *  @param placeholder   站位文字
 *  @param textColor     文字颜色
 */
- (void)textFieldWitPlaceholder:(NSString *)placeholder textColor:(UIColor *)textColor {
    // 占位文字
    self.attributedPlaceholder = [[NSAttributedString alloc] initWithString:placeholder attributes:@{NSForegroundColorAttributeName: textColor}];
}

/**
 *  设置文本框的样式
 *
 *  @param placeholder   站位文字
 *  @param textAlignment 文本对齐方式
 */
- (void)textFieldWitPlaceholder:(NSString *)placeholder textAlignment:(NSTextAlignment )textAlignment {
    
    [self textFieldWitPlaceholder:placeholder];
    // 设置文本对齐方式
    self.textAlignment = textAlignment;
}

/**
 *  设置文本框的样式
 *
 *  @param placeholder 站位文字
 *  @param imageName   图片名称
 */
//- (void)textFieldWithPlaceholder:(NSString *)placeholder borderColor:(UIColor *)borderColor imageName:(NSString *)imageName {

    // 设置文本框边框属性
//    self.layer.borderWidth = _borderWidth;
//    self.layer.cornerRadius = _cornerRadius;
//    
//    // 文本框的颜色
//    UIColor *color = [UIColor rgb:@"38831d" alpha:1];
//    self.layer.borderColor = [color CGColor];
//    
//    // 占位文字
//    self.attributedPlaceholder = [[NSAttributedString alloc] initWithString:placeholder attributes:@{NSForegroundColorAttributeName: color}];
//    
//    // 设置左边图片试图
//    CGFloat margin;
//    CGFloat top;
//    if (IPHONE6P) {  //   6plus
//        margin = 18;
//    } else if (IPHONE6){ //  6
//        margin = 14;
//    } else {
//        margin = 11;
//    }
//    UIImageView *imageView = [[UIImageView alloc] init];
//    imageView.image = [UIImage imageNamed:imageName];
//    [imageView sizeToFit];
//    top = (self.height - imageView.height) * 0.5;
//    
//    imageView.x = margin;
//    imageView.y = top;
//    CGFloat imageViewWidth = imageView.frame.size.width;
//    
//    // 设置一个空的左边view, 把输入范围往右边移动
//    self.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, margin + imageViewWidth + margin * 0.7, 0)];
//    self.leftViewMode = UITextFieldViewModeAlways;
//    [self addSubview:imageView];

//    UIView *clearView = [[UIView alloc] init];
//    UIButton *clearButton = [[UIButton alloc] init];
//    [clearButton setImage:[UIImage imageNamed:@"login_correct"] forState:UIControlStateNormal];
//    [clearButton sizeToFit];
//    [clearView addSubview:clearButton];
//    [clearView sizeToFit];
//    self.rightView = clearView;
//    self.rightViewMode = UITextFieldViewModeWhileEditing;
//}

/**
 *  设置文本框的样式
 *
 *  @param placeholder   站位文字
 *  @param textAlignment 文本对齐方式
 *  @param imageName     图片名称
 */
//- (void)textFieldWithPlaceholder:(NSString *)placeholder  textAlignment:(NSTextAlignment )textAlignment imageName:(NSString *)imageName {
//
//    [self textFieldWithPlaceholder:placeholder imageName:imageName];
//    // 设置文本对齐方式
//    self.textAlignment = textAlignment;
//}

/**
 *  设置文本框的样式
 *
 *  @param borderWitdh          边框宽度
 *  @param cornerRadius         圆角大小
 *  @param borderColor          边框颜色
 *  @param placeholder          站位文字
 *  @param placeHolderColor     站位文字颜色
 *  @param alignment            展位文字对齐方式
 *  @param textFieldHeight      文本框的高度
 */
- (void)textFieldWithBorderWidth:(CGFloat)borderWitdh cornerRadius:(CGFloat)cornerRadius borderColor:(UIColor *)borderColor placeholder:(NSString *)placeholder placeHolderColor:(UIColor *)placeHolderColor textAlignment:(NSTextAlignment)alignment textFieldHeight:(CGFloat)textFieldHeight {
    
    // 设置文本框边框属性
    self.layer.borderWidth = borderWitdh;
    self.layer.cornerRadius = cornerRadius;
    self.layer.borderColor = [borderColor CGColor];
	self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, textFieldHeight);
    
    // 占位文字
    self.attributedPlaceholder = [[NSAttributedString alloc] initWithString:placeholder attributes:@{NSForegroundColorAttributeName: placeHolderColor}];
}

/**
 *  设置文本框的样式
 *
 *  @param borderWitdh       边框宽度
 *  @param cornerRadius      圆角大小
 *  @param borderColor       边框颜色
 *  @param placeholder       站位文字
 *  @param imageName         左侧试图图片名称
 *
 *  @return 设置好后的文本框
 */
- (void)textFieldWithBorderWidth:(CGFloat)borderWitdh cornerRadius:(CGFloat)cornerRadius borderColor:(UIColor *)borderColor placeholder:(NSString *)placeholder imageName:(NSString *)imageName{
    
    // 设置文本框边框属性
    self.layer.borderWidth = borderWitdh;
    self.layer.cornerRadius = cornerRadius;
    self.layer.borderColor = [borderColor CGColor];
    
    // 设置左边图片试图
    CGFloat margin = 16;
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(margin, 11, 0, 0)];
    imageView.image = [UIImage imageNamed:imageName];
    [imageView sizeToFit];

    self.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, margin * 2 + margin, 0)];
    self.leftViewMode = UITextFieldViewModeAlways;
    [self addSubview:imageView];
    
    // 占位文字
    self.attributedPlaceholder = [[NSAttributedString alloc] initWithString:placeholder attributes:@{NSForegroundColorAttributeName: borderColor}];
}


@end
